package cn.itcast.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import cn.itcast.service.impl.PersonServiceBean2;


public class JDKProxyFactory implements InvocationHandler {
	private Object targetObject;

	public Object createProxyInstance(Object targetObject) {
		this.targetObject = targetObject;
		
		return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),
				this.targetObject.getClass().getInterfaces(), this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		PersonServiceBean2 bean2 = (PersonServiceBean2) this.targetObject;
		
		Object result = null;
		if (bean2.getUser() != null) {
			result = method.invoke(targetObject, args);
		}
		
		return result;
	}
}
